package com.yndsoft.jc.ui.memberpage.address;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseActivity;
import com.yndsoft.jc.model.Order;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.utils.ToastUtil;
import com.yndsoft.jc.ui.memberpage.address.entity.AddressBean;
import com.yndsoft.jc.ui.memberpage.myorder.MemberAddress;
import com.yndsoft.jc.ui.net.util.ProgressSubscriber;
import com.yndsoft.jc.utils.ConstantValues;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by hasee on 2017/12/7.
 */

public class MailingAddressActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.base_backBtn)
    RelativeLayout base_backBtn;
    @Bind(R.id.base_title)
    TextView base_title;


    @Bind(R.id.mailing_address_list)
    ListView mailing_address_list;
    List<MemberAddress> mData = new ArrayList<>();
    MailingAddressAdapter adapter;
    View footView;
    Button mailing_address_add_address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mail_address);

        setViews();
        setDatas();
        setListener();
    }

    private void setDatas() {
        mHttpUtil.getMailAds(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                Type type = new TypeToken<List<MemberAddress>>() {
                }.getType();
                Object fromJson = new Gson().fromJson(returnMsg.getInfo(), type);
                mData = (ArrayList<MemberAddress>) fromJson;
                adapter.setDatas(mData);
            }
        }, "addresslist", mSession.getUserInfo().getId());
    }

    private void setViews() {
        base_title.setText(getResources().getString(R.string.mailing_address));
        footView = LayoutInflater.from(mContext).inflate(R.layout.layout_mailing_address_addbtn, null);
        mailing_address_add_address = footView.findViewById(R.id.mailing_address_add_address);
        adapter = new MailingAddressAdapter(mContext);
        mailing_address_list.setAdapter(adapter);
        mailing_address_list.addFooterView(footView);
//        for (int o = 0; o < mData.size(); o++) {
//            if (mData.get(o).getIsDefault() != null && mData.get(o).getIsDefault().equals("true")) {
//                adapter.setSelectPosition(o);
//            }
//        }
    }

    private void setListener() {
        base_backBtn.setOnClickListener(this);
        adapter.setMyCallBack(new MailingAddressAdapter.CallBack() {
            @Override
            public void click(int position) {
                deleteAddress(position);
            }

            @Override
            public void editClick(MemberAddress item) {
                Intent intent = new Intent(MailingAddressActivity.this, EditAddressActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("addressInfo", item);
                intent.putExtra("adrsBundle", bundle);
                startActivityForResult(intent, ConstantValues.ADDRESS_REQUEST_CODE);
            }

            @Override
            public void defaultClick(int position) {
                setDefaultAddress(position);
            }
        });
        mailing_address_add_address.setOnClickListener(this);
//        mailing_address_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                setDefaultAddress(i);
//            }
//        });
    }

    private void setDefaultAddress(int position) {
        MemberAddress bean = mData.get(position);
        bean.setIsdefault(true);
        mHttpUtil.setDefaultAds(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        ToastUtil.show(mContext, "设置失败");
                        break;
                    case 1:
                        setDatas();
                        setResult(RESULT_OK);
                        ToastUtil.show(mContext, "设置成功");
                        break;
                }
            }
        }, "defaultaddress", mSession.getUserInfo().getId(), bean.getId());
    }

    private void deleteAddress(int position) {
        mHttpUtil.deleteAddress(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        ToastUtil.show(mContext, "删除失败");
                        break;
                    case 1:
                        setDatas();
                        ToastUtil.show(mContext, "删除成功");
                        break;
                }
            }
        }, "deleteaddress", mSession.getUserInfo().getId(), mData.get(position).getId());
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.base_backBtn:
                finish();
                break;
            case R.id.mailing_address_add_address:
                intent = new Intent(this, AddMailAddressActivity.class);
                startActivityForResult(intent, ConstantValues.ADDRESS_ADD_NEW);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        setDatas();
    }
}
